package com.baizhi.service.impl;

import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.dao.AdminDao;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;


    @Override
    @AddCacheAnnotation
    public Admin login(String userName, String password) {
      /* boolean b=false;*/
        System.out.println(userName+"---"+password);
        Admin a=adminDao.selectByUserName(userName);
        System.out.println(a);
        if(a.getPassword().equals(password)&&userName!=null){
            return a;
        }else{
            return null;
        }



    }

    @Override
    public Map selectAllAdmin(int page, int limit) {
        int offset=(page-1)*limit;
        Map map=new HashMap();
        List<Admin> list=adminDao.selectAllAdmin(offset,limit);
        int count=adminDao.selectAdmin();
        map.put("data",list);
        map.put("count",count);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    @Override
    public Map insertAdmin(Admin admin) {
        Map map=new HashMap();
        int count=adminDao.insertAdmin(admin);
        if(count>0){
            map.put("isAdd",true);
        }else{
            map.put("isAdd",false);
        }
        return map;
    }

    @Override
    public Map updataAdmin(Admin admin) {
        Map map=new HashMap();
        int count=adminDao.updataAdmin(admin);
        if(count>0){
            map.put("isUpdate",true);
        }else{
            map.put("isUpdate",false);
        }
        return map;
    }

    @Override
    public Admin selectById(int id) {
        return adminDao.selectById(id);
    }

    @Override
    public Map deleteAdmin(int id) {
        Map map=new HashMap();
        int count =adminDao.deleteAdmin(id);
        if(count>0){
            map.put("isDelete",true);
        }else{
            map.put("isDelete",false);
        }
        return map;
    }

    @Override
    public Admin selectByUserName(String userName) {
        return adminDao.selectByUserName(userName);
    }

    @Override
    public Set<String> selectRole(String userName) {
        return adminDao.selectRole(userName);
    }

    @Override
    public Set<String> selectResource(String userName) {
        return adminDao.selectResource(userName);
    }
}
